package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.textview.EllipsezeTextView;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/3/22.
 */

public class MainPicDiscoverHolder extends BaseDiscoverHolder {

    private ImageView mainPicImg;
    private TextView pic_num;
    private EllipsezeTextView intro;

    public MainPicDiscoverHolder(View itemView) {
        super(itemView);
        mainPicImg = (ImageView) itemView.findViewById(R.id.ground_content_img);
        pic_num = (TextView) itemView.findViewById(R.id.ground_pagenum_tv);
        intro = (EllipsezeTextView) itemView.findViewById(R.id.ground_intro_tv);
    }

    @Override
    public void renderData(final Timeline item, final Context context) {
        super.renderData(item, context);
        ImageLoader.getInstance().displayImage(item.getContentAddress(), mainPicImg, ImageOptions.optionsTimeline);
        if (!TextUtils.isEmpty(item.getPicNum())) {
            if (Integer.parseInt(item.getPicNum()) > 1) {
                pic_num.setVisibility(View.VISIBLE);
            } else {
                pic_num.setVisibility(View.GONE);
            }
        } else {
            pic_num.setVisibility(View.GONE);
        }
        String defaultIntro = TimelineFactory.whichType(item) == TimelineFactory.TIMELINE_TYPE_COSER ? context.getString(R.string.post_cos) : context.getString(R.string.post_draw);
        setRecommendIntro(context, intro, item, item.getIntro(), defaultIntro);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setHotViewClick(item, context);
            }
        });
        intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setHotViewClick(item, context);
            }
        });
    }

    private void setRecommendIntro(Context mContext, EllipsezeTextView tvIntro, Timeline mItem, String intro, String defaultIntro) {
        String finalIntro = TextUtils.isEmpty(intro) ? defaultIntro : intro;
        finalIntro = String.format(mContext.getResources().getString(R.string.card_recommended_user_format), mItem.getOUName(), finalIntro);
        tvIntro.setText(finalIntro, mItem.getOUName(), mItem.getOuid(), true);
        tvIntro.setVisibility(View.VISIBLE);
    }

    private void setHotViewClick(Timeline item, Context context) {
        switch (TimelineFactory.whichType(item)) {
            case TimelineFactory.TIMELINE_TYPE_COSER:
                DetailDataHelper.gotoCosDetail(context, item.getRp_id(), item.getCp_id(), false);
                break;
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
                DetailDataHelper.gotoDrawerDetail(context, item.getRp_id(), item.getDp_id(), false);
                break;
        }
    }

}
